package com.flightan.app.parser;

import com.flightan.app.model.Flight;
import com.flightan.app.utils.DateParser;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

/**
 * User: Treshchev
 * Date: 23.04.12
 * Time: 14:46
 */
public class SheremetyevoParser
{
    private static final String DATE_FORMAT = "HH:mm yyyy dd MMMMM";

    private static final String FLIGHTS_URL = "http://www.svo.aero/timetable/today/";


    private String clearHTMLCode(String str)
    {
        char nbsp = 160;
        char space = 32;
        return str.replace(nbsp, space);

    }

    public List<Flight> getArrivalFlights()
    {
        List<Flight> arrivalFlights = new ArrayList<Flight>();
        try
        {
            Document doc = Jsoup.connect(FLIGHTS_URL).get();
            Elements trList = doc.getElementsByClass("sA");
            for (Element tr : trList)
            {
                Elements td = tr.getElementsByTag("td");
                Flight flight = new Flight();

                if (td.size()>5)
                {
                    StringBuilder date = new StringBuilder();
                    date.append(td.get(1).text()).append(" ");
                    date.append(String.valueOf(Calendar.getInstance().get(Calendar.YEAR))).append(" ");
                    date.append(clearHTMLCode(td.get(0).text()));
                    flight.setArrivalDate(DateParser.parseTime(date.toString(), DATE_FORMAT, new Locale("ru","RU")));
                    flight.setFlight(td.get(2).text());
                    flight.setCode(td.get(3).text());
                    flight.setFrom(td.get(5).text());
                    flight.setGate(td.get(6).text());
                    flight.setNotes(td.get(7).text());
                    arrivalFlights.add(flight);
                }
            }
        }
        catch (IOException e)
        {
            System.out.println(e.getMessage());
        }
        return arrivalFlights;
    }

    public List<Flight> getFlights()
    {
        List<Flight> departureFlights = new ArrayList<Flight>();
        try
        {
            Document doc = Jsoup.connect(FLIGHTS_URL).get();
            Elements trList = doc.getElementsByClass("sD");
            for (Element tr : trList)
            {

                Elements td = tr.getElementsByTag("td");
                Flight flight = new Flight();

                if (td.size()>5)
                {
                    StringBuilder date = new StringBuilder();
                    date.append(td.get(1).ownText()).append(" ");
                    date.append(String.valueOf(Calendar.getInstance().get(Calendar.YEAR))).append(" ");
                    date.append(clearHTMLCode(td.get(0).ownText()));
                    flight.setArrivalDate(DateParser.parseTime(date.toString(), DATE_FORMAT, new Locale("ru","RU")));
                    flight.setFlight(td.get(2).text());
                    flight.setCode(td.get(3).text());
                    flight.setTo(td.get(5).text());
                    flight.setGate(td.get(6).text());
                    flight.setNotes(td.get(7).text());
                    departureFlights.add(flight);
                }

            }
        }
        catch (IOException e)
        {
            System.out.println(e.getMessage());
        }
        return departureFlights;
    }

}
